-- Adaptive Replacement Cache implementation in Lua
--

local function _randomwait ()
    local c = 0
    for i = 1, math.random(1e7, 5e7) do
        c = c + 1
    end
    return c
end


local Disk_t = {
    __index = function (this, key)
        _randomwait()
        return rawget(this, key)
    end,

    __newindex = function (this, key, value)
        _randomwait()
        return rawset(this, key, value)
    end,
}


function Disk ()
    return setmetatable({}, Disk_t)
end


local Arc_t = {
    __index = function (this, key)
        local c = rawget(this, 'c')
        c.mru[key] = true
        table.insert(c.mru, key)
    end,

    __newindex = function (this, key, value)
        local c = rawget(this, 'c')
    end,
}


function Arc (disk)
    disk = disk or Disk()
    local t = {
        _disk = disk
        c = {
            cache = {}
            mru  = {}
            gmru = {}
            mfu  = {}
            gmfu = {}
        }
    }
    return setmetatable(t, Arc_t)
end


disk = Disk()
diskarc = Arc(disk)


-- vim: set sw=4 sts=4 et:
